package com.blueocean_health.care.management.mapper;

import com.blueocean_health.care.management.domain.base.OrderServiceInfo;
import com.blueocean_health.care.management.domain.dto.*;
import com.blueocean_health.care.management.util.MyMapper;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

@Mapper
public interface HomeCalcMapper extends MyMapper<HomeCalcOrderInfo> {


    //根据当前team_id查下级所有的team_id
    @Select("SELECT team_id FROM team WHERE `code` LIKE CONCAT_WS('', (SELECT REPLACE(`code`,'000','') pCode FROM team WHERE team_id=#{teamId}),'%')")
    List<Integer> queryHospitalListByTeamId(String teamId);

    @Select({
        "SELECT ",
        "   a.order_id orderId, a.patient_name patientName, a.phone, a.hospital_id hospitalId, a.create_time createTime, ",
        "   b.service_id serviceId, b.begin_time beginTime, b.end_time endTime, b.actual_total_price actualTotalPrice, b.one_day_price oneDayPrice, b.actual_day_price actualDayPrice, b.one_hour_price oneHourPrice, b.order_type_flag orderTypeFlag, b.order_type orderType, b.refund_service_id refundServiceId, b.service_type serviceType, ",
        "   c.worker_id workerId, c.total_pay totalPay, c.one_day_pay oneDayPay, c.one_hour_pay oneHourPay, ",
        "   e.proportions, e.group_id groupId, ",
        "   f.post_subsidy postSubsidy ",
        "FROM (SELECT a.order_id, a.version, a.patient_name, a.phone, a.hospital_id, a.create_time, a.deflag FROM (SELECT * FROM order_info ORDER BY version DESC) a GROUP BY a.order_id) a ",
        "LEFT JOIN (SELECT b.service_id, b.order_id, b.begin_time, b.end_time, b.actual_total_price, b.one_day_price, b.actual_day_price, b.one_hour_price, b.order_type_flag, b.order_type, b.refund_service_id, b.service_type, b.version FROM order_service_info b ) b ",
        "ON (a.order_id=b.order_id AND a.version=b.version) ",
        "LEFT JOIN order_service_nurse_info c ON (b.service_id=c.service_id AND a.version=c.version) ",
        "LEFT JOIN (SELECT * FROM (SELECT * FROM nurse_group_relation ORDER BY version DESC) a GROUP BY a.nurse_id) d ON c.worker_id=d.nurse_id ",
        "LEFT JOIN nurse_group e ON c.worker_id=e.group_id ",
        "LEFT JOIN nurse_group_percentages f ON c.worker_id=f.nurse_id ",
        "WHERE a.deflag=0 AND c.cancel=0 AND a.create_time<#{nowStartTime}"
    })
    List<HomeCalcOrderInfo> queryList(Date nowStartTime);


    //查询所有的退款订单
    @Select({"SELECT a.order_id orderId, hospital_id hospitalId, IF(a.fee_type = 3, a.patient_sign_date, a.billing_time) time, a.service_id serviceId FROM order_charge_info a WHERE is_active=1 AND a.time < #{nowStartTime}"})
    List<HomeCalcRefundInfo> queryRefundList(Date nowStartTime);

    @Delete("TRUNCATE TABLE `home_calc_data`")
    long deleteData();

    //批量插入计算数据
    @Insert({"<script>",
            "INSERT INTO `home_calc_data` (`time`, `patient_count`, `nurse_count`, `receive_amount`, `payment_amount`, `commission_amount`, `gross_profit_margin`, `create_time`) VALUES ",
            "<foreach collection =\"list\" item=\"item\" index= \"index\" separator =\",\">",
                "(#{item.time}, #{item.patientCount}, #{item.nurseCount}, #{item.receiveAmount}, #{item.paymentAmount}, #{item.commissionAmount}, #{item.grossProfitMargin}, NOW()) ",
            "</foreach >",
    "</script>"})
    int insertBatchDate(@Param("list") List<HomeCalcRespInfo> data);

    @Select("SELECT time label, time `value` FROM `home_calc_data` WHERE LENGTH(time) < 10 ORDER BY time")
    List<HomeCalcType> queryType();

    @Select("SELECT time, patient_count patientCount, nurse_count nurseCount, receive_amount receiveAmount, payment_amount paymentAmount, commission_amount commissionAmount, gross_profit_margin grossProfitMargin FROM home_calc_data WHERE time LIKE CONCAT_WS('',#{value},'%') ORDER BY time DESC")
    List<HomeCalcRespInfo> queryData(HomeCalcRequest request);

    @Select("SELECT actual_total_price FROM order_service_info WHERE refund_service_id=#{serviceId} AND cancel=0 AND order_type=3")
    Double queryPrice(String serviceId);

    @Select({"SELECT c.order_id orderId, a.service_id serviceId, a.begin_time beginTime, a.end_time endTime, b.worker_id workerId FROM order_service_info a LEFT JOIN order_service_nurse_info b ON a.service_id=b.service_id LEFT JOIN order_info c ON a.order_id=c.refund_order_id WHERE a.service_id=#{serviceId}"})
    List<HomeCalcOrderInfo> queryServiceInfoByServiceId(String serviceId);

}
